package com.zk.wx.service;

import com.zk.wx.model.commons.TextMessage;
import com.zk.wx.model.reps.Article;
import com.zk.wx.model.reps.NewsMessage;
import com.zk.wx.util.MessageUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by 15911 on 2017/10/15.
 */
public class CoreService {

    static final Logger logger = LoggerFactory.getLogger(CoreService.class);
    /**
     * 处理微信发来的请求
     * @param request
     * @return xml
     */
    public static String processRequest(HttpServletRequest request) {
        // xml格式的消息数据
        String respXml = "success";
        // 默认返回的文本消息内容
        try {
            // 调用parseXml方法解析请求消息
            Map<String, String> requestMap = MessageUtil.parseXml(request);
            // 发送方帐号
            String fromUserName = requestMap.get("FromUserName");
            // 开发者微信号
            String toUserName = requestMap.get("ToUserName");
            // 消息类型
            String msgType = requestMap.get("MsgType");


            logger.info("request param=" + requestMap.toString());

            //消息为text类型
            if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)){
                String content = requestMap.get("Content");
                if(content.equals("1")){
                    String titel = "↓↓↓点击进入影院↓↓↓";
                    String des = "如果你觉得有意思，那就把屁屁分享给朋友吧！！";
                    String url = "http://mrzkblog.top";
                    respXml = reqText(content,fromUserName,toUserName,titel,des,url);
                    logger.info("response data=" + respXml);
                }else{
                    String titel = "屁屁在影院找到《" + content + "》";
                    String des = "点击↑↑↑开始看电影吧↑↑↑";
                    String url = "http://mrzkblog.top/seacher.php?wd=" + content;
                    respXml = reqText(content,fromUserName,toUserName,titel,des,url);
                    logger.info("response data=" + respXml);
                }
            }
            //消息为事件类型
            else if(msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)){
                String event = requestMap.get("Event");
                if(MessageUtil.EVENT_TYPE_SUBSCRIBE.equals(event)){
                    // 回复文本消息
                    TextMessage textMessage = new TextMessage();
                    textMessage.setToUserName(fromUserName);
                    textMessage.setFromUserName(toUserName);
                    textMessage.setCreateTime(new Date().getTime());
                    textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
                    textMessage.setContent("欢迎关注【屁屁影院】\n同步更新VIP影视资源\n\n回复【1】进入影院\n回复【电影名称】直接获取电影\n\n温馨提示：如果资源卡顿或不能播放，请尝试切换线路，共有10个线路\n\n把我分享到朋友圈和小伙伴们一起看吧！！");
                    // 将文本消息对象转换成xml
                    respXml = MessageUtil.messageToXml(textMessage);
                }
            }else{
                // 回复文本消息
                TextMessage textMessage = new TextMessage();
                textMessage.setToUserName(fromUserName);
                textMessage.setFromUserName(toUserName);
                textMessage.setCreateTime(new Date().getTime());
                textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
                textMessage.setContent("欢迎关注【屁屁影院】\n同步更新VIP影视资源\n\n回复【1】进入影院\n回复【电影名称】直接获取电影\n\n温馨提示：如果资源卡顿或不能播放，请尝试切换线路，共有10个线路\n\n把我分享到朋友圈和小伙伴们一起看吧！！");
                // 将文本消息对象转换成xml
                respXml = MessageUtil.messageToXml(textMessage);
            }
        } catch (Exception e) {
            logger.error("ERROR==" + e.getMessage());
        }
        return respXml;
    }

    /**
     * 文本消息请求处理
     * @param content
     * @param fromUserName
     * @param toUserName
     * @return
     */
    public static String reqText(String content,String fromUserName,String toUserName,String title,String des,String url){
        //回复图文消息
        Article article = new Article();
        //标题
        article.setTitle(title);
        //描述
        article.setDescription(des);
        //图片链接
        article.setPicUrl("http://mrzkblog.top/ss.jpg");
        //点击跳转连接
        article.setUrl(url);

        NewsMessage message = new NewsMessage();
        message.setToUserName(fromUserName);
        message.setFromUserName(toUserName);
        message.setCreateTime(new Date().getTime());
        message.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
        message.setArticleCount(1);
        List list = new ArrayList<>();
        list.add(article);
        message.setArticles(list);

        // 将文本消息对象转换成xml
        String respXml = MessageUtil.messageToXml(message);
        return respXml;
    }
}
